//处理数据获取，数据只流向vuex的store
import store from '@/store/index.js';
import axios from 'axios';
export default {
    // 登录
    login: function (clientName, clientPassword) {
        console.log('data.js----------login');
        store.commit("savelogin", false);
        axios
            .get("/user/login", {
                params: {
                    name: clientName,
                    password: clientPassword,
                },
            })
            .then((response) => {
                var clientId = response.data;
                if (clientId.length <= 7) {
                    console.log("登录失败");
                    return false;
                } else {
                    console.log("登录成功");
                    //向store传递数据
                    store.commit("saveId", clientId);
                    store.commit("saveName", clientName);
                    store.commit("savelogin", true);
                    return true;
                }
            })
            .catch((error) => {
                //console.log(error);
            });
    },
    // 注册
    register(clientName, clientPassword) {
        console.log('data.js----------register');
        axios
            .get("/user/register", {
                params: {
                    name: clientName,
                    password: clientPassword,
                },
            })
            .then((response) => {
                var flag = response.data;
                if (flag == "OK") {
                    console.log("注册成功");
                    //自行登录
                    this.login(clientName, clientPassword);
                } else {
                    console.log("注册失败");
                }
            })
            .catch((error) => {
                //console.log(error);
            });
    },
    //获取购物车数据，并处理图片路径
    getCar() {
        console.log('data.js----------getCar');
        axios
            .get("/cart/listByUser", {
                params: {
                    userId: store.state.clientId,
                },
            })
            .then((response) => {
                store.commit("saveCart", response.data);
                console.log(response.data);
                var CarImgAddress = [];
                for (var i = 0; i < response.data.length; i++) {
                    CarImgAddress.push(
                        "http://202.193.53.235:8080/" + response.data[i].thumbnail
                    );

                }
                store.commit("saveCarImgAddress", CarImgAddress);
            })
            .catch((error) => {
                //console.log(error);
            });
    },
    //获取购物车表格数据，大处理
    getTable() {
        console.log('data.js----------getTable');
        var tableData = [];
        axios
            .get("/cart/listByUser", {
                params: {
                    userId: store.state.clientId,
                },
            })
            .then((response) => {
                var Data = response.data;
                for (var i = 0; i < Data.length; i++) {
                    var name = Data[i].name;
                    var num = parseInt(Data[i].num);
                    var price = parseInt(Data[i].price);
                    var imgurl = "http://202.193.53.235:8080/" + Data[i].thumbnail;
                    var smallSum = num * price;
                    var cartId = Data[i].cardid;
                    var json = {};
                    json.name = name;
                    json.num = num;
                    json.price = price;
                    json.imgurl = imgurl;
                    json.smallSum = smallSum;
                    json.carid = cartId;
                    tableData.push(json);    
                }
                store.commit("saveCartTable", tableData);
            })
            .catch((error) => {
                //console.log(error);
            });
    },
    //从购物车删除商品，在删除后会调用getCar()和getTable()更新数据
    //http://[ip]:[port]/cart/deleteById?userId=[userId]&cartId=[userId]
    deleteCar(cartId) {
        console.log('data.js----------deleteCar');
        axios.get('/cart/deleteById',{
            params:{
              userId:'aef1a027e55a45698bb8d2942b0fbaa5',
              cartId:cartId
            }
          }).then((response)=>{
            console.log(response);
            this.getTable();
            this.getCar();
          }).catch((error)=>{
            console.log(error);
          });
    }

}